import Ember from 'ember';
import Changeset from 'ember-changeset';
import customerbusinessdetailValidations from '../../../validations/customerbusinessdetail';
import lookupValidator from 'ember-changeset-validations';
const {leaveStatus1,consultStatus5,consultStatus6,consultStatus7} = Constants;

export default Ember.Controller.extend(customerbusinessdetailValidations,{
  dateService: Ember.inject.service("date-service"),
  dataLoader: Ember.inject.service("data-loader"),
  customerflowdModel:Ember.computed('customerflow',function(){
    var model = this.get("customerflow");
    if (!model) {
        return null;
    }
    return new Changeset(model, lookupValidator(customerbusinessdetailValidations), customerbusinessdetailValidations);
  }),
  defaultOldName:Ember.computed('customerflow.customer',function(){
      return this.get('customerflowdModel.customer');
  }),
  defaultName:Ember.computed('customerflow.leaveStaff',function(){
      return this.get('customerflow.leaveStaff');
  }),
  todayDate:Ember.computed(function(){
    var nowTime = new Date();
    return nowTime;
  }),
  actions:{
    invalid() {
        //alert("error");
    },
    back:function(){
      var mainpageController = App.lookup('controller:business.mainpage');
      mainpageController.switchMainPage('stay-back');
    },
    changeAction(date) {
        var stamp = this.get("dateService").getLastSecondStampOfDay(date);
        this.set("customerflowdModel.leaveRecordDate", stamp);
    },
    changeLeaveAction(date) {
        var stamp = this.get("dateService").getLastSecondStampOfDay(date);
        this.set("customerflowdModel.leaveDate", stamp);
    },
    //保存
    detailSaveClick: function(customerflow) {
      var editMode = this.get('editMode');
      var customerflowdModel = this.get('customerflowdModel');
      var _self = this;
      customerflowdModel.validate().then(function(){
        if(customerflowdModel.get('errors.length')===0){
          if(editMode=='add'){
            var leaveStatusObj = _self.get("dataLoader").findDict(Constants.leaveStatus1),
                statusObj = _self.get("dataLoader").findDict(Constants.consultStatus6);
            customerflowdModel.set("leaveStatus",leaveStatusObj);//设置为申请退住
            customerflowdModel.set("status",statusObj);//设置为退住
            App.lookup('controller:business.mainpage').openPopTip("正在保存");
            customerflowdModel.save().then(function(){
              App.lookup('controller:business.mainpage').showPopTip("保存成功");
              var mainpageController = App.lookup('controller:business.mainpage');
              mainpageController.switchMainPage('stay-back');
            });

          }else{
            App.lookup('controller:business.mainpage').openPopTip("正在保存");
            customerflowdModel.save().then(function(){
              App.lookup('controller:business.mainpage').showPopTip("保存成功");
              _self.set('detailEdit',false);
            });
          }

        }else{
          customerflowdModel.set("validFlag",Math.random());
        }
      });

    },
    //编辑按钮
    detailEditClick:function(){
      this.set('detailEdit',true);
    },
    //取消
    detailCancel:function(){
      var id=this.get('id');
      var editMode=this.get('editMode');
      if(editMode=='edit'){
        this.set('detailEdit',false);
        this.get('customerflow').rollbackAttributes();
      }else{
        console.log("11111111111detailEdit  else");
        this.get('customerflow').rollbackAttributes();
        var mainpageController = App.lookup('controller:business.mainpage');
        mainpageController.switchMainPage('stay-back');
      }

    },
    //选择老人
    selectOldman(customer) {
        // this.set('customerflow.customer',customer.get("customer"));
        // this.set('customerflowdModel.customer',customer.get("customer"));
        this.set('customerflow',customer);
    },
    //选择登记人
    selectName(user) {
        this.set('customerflow.leaveStaff',user);
        this.set('customerflowdModel.leaveStaff',user);
    },
    //删除按钮
    delById : function() {
      var _self = this;
      App.lookup('controller:business.mainpage').showConfirm("是否确定删除此床位记录",function(){
        _self.send('cancelPassSubmit',_self.get('customerflowdModel'));
      });
    },
    //弹窗取消
    invitation(){
      this.set('showpopInvitePassModal',false);
    },
    //弹窗确定，删除记录
    cancelPassSubmit(customerflow){
      App.lookup('controller:business.mainpage').openPopTip("正在删除");
			this.set("showpopInvitePassModal",false);
      customerflow.set("delStatus", 1);
      customerflow.save().then(function() {
          App.lookup('controller:business.mainpage').showPopTip("删除成功");
          var mainpageController = App.lookup('controller:business.mainpage');
          mainpageController.switchMainPage('stay-back');
      });
		},

  }
});
